package org.lanqiao.webfm.dao;

import java.util.*;
import java.io.Serializable;
import java.sql.*;
import java.lang.reflect.*;

import javacommon.base.*;
import javacommon.ijdbc.*;
import javacommon.util.*;
import javacommon.web.page.*;

import org.lanqiao.webfm.model.*;

import org.slf4j.*;

/**
*@Author zhengwei
**/
public class GoodsDao extends BaseJdbcDao<Goods>{
	private static final Logger LOGGER = LoggerFactory.getLogger(GoodsDao.class);
	private GoodsDao(){}
	private static GoodsDao instance = new GoodsDao();
	public static GoodsDao getInstance(){
		return instance;
	}
	public Class getEntityClass() {
		return Goods.class;
	}
	
	/*public RowMapper<Goods> getMapper() {
		return new GoodsMapper();
	}*/
	public String getIdColumnName() {
		return "ID";
	}
	public String getTableName() {
		return "SP_GOODS";
	}
	
	public Goods getById(Serializable id) throws SQLException {
		String sql = "select * from SP_GOODS t1 "
				+ "where t1.ID=?";
        return operation.queryForUniqueBean(sql, getEntityClass(), id);
	}
	@Override
	public void save(Goods entity) throws SQLException {
		StringBuilder sql = new StringBuilder("insert into SP_GOODS")
				.append(" (")
				.append("ID").append(",")
				.append("NAME").append(",")
				.append("TYPE_ID").append(",")
				.append("PRICE").append(",")
				.append("DISCOUNT").append(",")
				.append("DESCP").append(",")
				.append("SUPPLIER").append(",")
				.append("QTY").append(",")
				.append("ISPREFERRED").append(",")
				.append("PIC")
				.append(") ")
				.append("values(dev_seq.nextval,")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?").append(",")
				.append("?")
				.append(")");
			
		super.operation
				.execute(sql.toString(), 
								entity.getName(),
								entity.getTypeId(),
								entity.getPrice(),
								entity.getDiscount(),
								entity.getDescp(),
								entity.getSupplier(),
								entity.getQty(),
								entity.getIspreferred(),
								entity.getPic()
				);
	}
	@Override
	public void update(Goods entity) throws SQLException {
		StringBuilder sql = new StringBuilder("update SP_GOODS")
				.append(" set ") 
				.append("NAME=?").append(",")
				.append("TYPE_ID=?").append(",")
				.append("PRICE=?").append(",")
				.append("DISCOUNT=?").append(",")
				.append("DESCP=?").append(",")
				.append("SUPPLIER=?").append(",")
				.append("QTY=?").append(",")
				.append("ISPREFERRED=?").append(",")
				.append("PIC=?")
				.append(" where ID=?");
		super.operation
				.execute(sql.toString(),
							entity.getName(),
							entity.getTypeId(),
							entity.getPrice(),
							entity.getDiscount(),
							entity.getDescp(),
							entity.getSupplier(),
							entity.getQty(),
							entity.getIspreferred(),
							entity.getPic(),
						entity.getId());
	}
	/**
	 * 根据query对象来查询数据，结果取决于query中有查询条件及query中的分页信息
	 */
	public Page<Goods> findPage(PageRequest<Goods> query) throws SQLException{
		Goods goods = (Goods) query;
        StringBuilder sql = new StringBuilder("select t.* from SP_GOODS t where 1=1 ");
        if(StringUtils.hasLength(goods.getId())) {
         	sql.append(" and  t.ID =  ").append(goods.getId());
        }
        if(StringUtils.hasLength(goods.getName())) {
            sql.append(" and  t.NAME =  '").append(goods.getName()).append("'");
        }
        if(StringUtils.hasLength(goods.getTypeId())) {
         	sql.append(" and  t.TYPE_ID =  ").append(goods.getTypeId());
        }
        if(StringUtils.hasLength(goods.getPrice())) {
         	sql.append(" and  t.PRICE <  ").append(goods.getPrice());
        }
        if(StringUtils.hasLength(goods.getDiscount())) {
         	sql.append(" and  t.DISCOUNT =  ").append(goods.getDiscount());
        }
        if(StringUtils.hasLength(goods.getDescp())) {
            sql.append(" and  t.DESCP =  '").append(goods.getDescp()).append("'");
        }
        if(StringUtils.hasLength(goods.getSupplier())) {
            sql.append(" and  t.SUPPLIER =  '").append(goods.getSupplier()).append("'");
        }
        if(StringUtils.hasLength(goods.getQty())) {
         	sql.append(" and  t.QTY =  ").append(goods.getQty());
        }
        if(StringUtils.hasLength(goods.getIspreferred())) {
            sql.append(" and  t.ISPREFERRED =  '").append(goods.getIspreferred()).append("'");
        }
        if(StringUtils.hasLength(goods.getPic())) {
            sql.append(" and  t.PIC =  '").append(goods.getPic()).append("'");
        }
        if(StringUtils.hasLength(goods.getSortColumns())) {
            sql.append(" order by ").append(goods.getSortColumns());
        }	
        
		return pageQuery(sql.toString(),goods);
	}
	
	public void deleteByIds(String[] ids) throws SQLException {
		String[] sqls = new String[ids.length];
		for (int i = 0; i < sqls.length; i++) {
			sqls[i] = "delete from SP_GOODS where ID=" + ids[i];
		}
		operation.executeBatch(sqls);
	}

}
